/*
 * Copyright 2018 JessYan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.jessyan.armscomponent.app.mvp.ui.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.armscomponent.app.R;
import me.jessyan.armscomponent.app.mvp.ui.adapter.LeftListAdapter;
import me.jessyan.armscomponent.app.mvp.ui.adapter.RightListAdapter;
import me.jessyan.armscomponent.app.mvp.ui.holder.LeftListHolder;
import me.jessyan.armscomponent.app.mvp.ui.holder.RightListHolder;
import me.jessyan.armscomponent.app.mvp.ui.model.LeftListBean;
import me.jessyan.armscomponent.app.mvp.ui.model.RightListBean;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;
import me.jessyan.armscomponent.commonsdk.utils.Utils;
import me.jessyan.armscomponent.commonservice.gank.service.GankInfoService;
import me.jessyan.armscomponent.commonservice.gold.service.GoldInfoService;
import me.jessyan.armscomponent.commonservice.zhihu.service.ZhihuInfoService;

/**
 * ================================================
 * 宿主 App 的主页
 *
 * @see <a href="https://github.com/JessYanCoding/ArmsComponent/wiki">ArmsComponent wiki 官方文档</a>
 * Created by JessYan on 19/04/2018 16:10
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * ================================================
 */
@Route(path = RouterHub.APP_MAINACTIVITY)
public class MainActivity extends BaseActivity {
    @BindView(R.id.rv_left_list)
    RecyclerView mLeftList;
    @BindView(R.id.rv_right_list)
    RecyclerView mRightList;

    private LeftListAdapter mLeftAdapter;
    private RightListAdapter mRightAdapter;

    private List<LeftListBean> mLeftData;
    private List<RightListBean> mRightData;
    private long mPressedTime;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {

    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_main;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        //ARouter.getInstance().inject(this);
        //这里想展示组件向外提供服务的功能, 模拟下组件向宿主提供一些必要的信息, 这里为了简单就直接返回本地数据不请求网络了
        initData();
        ArmsUtils.configRecyclerView(mLeftList, new LinearLayoutManager(this));
        ArmsUtils.configRecyclerView(mRightList,new LinearLayoutManager(this));
        mLeftAdapter = new LeftListAdapter(mLeftData, new LeftListHolder.LeftListItemClickListener() {
            @Override
            public void onItemClick(LeftListBean leftListBean) {
                switch ((int) leftListBean.getId()){
                    case 0:
                        changeRobotData();
                        break;
                    case 1:
                        changeToothData();
                        break;
                    case 2:
                        changeCarData();
                        break;
                }
            }
        });
        mLeftList.setAdapter(mLeftAdapter);
        mRightAdapter = new RightListAdapter(mRightData);
        mRightList.setAdapter(mRightAdapter);
    }

    private void initData(){
        mLeftData = new ArrayList<>();
        mLeftData.add(new LeftListBean(0,"AI视觉机器人",R.mipmap.icon_roblt));
        mLeftData.add(new LeftListBean(1,"套件",R.mipmap.icon_tooth));
        mLeftData.add(new LeftListBean(2,"智能小车",R.mipmap.icon_car));
        mLeftData.add(new LeftListBean(3,"关于应用",R.mipmap.icon_about));
        mLeftData.add(new LeftListBean(4,"联系我们",R.mipmap.icon_connect));
        changeRobotData();
    }

    private void changeRobotData(){
        if(mRightData == null){
            mRightData = new ArrayList<>();
        }
        mRightData.clear();
        mRightData.add(new RightListBean(0,"6WD ROBOT","(Android)",R.mipmap.img_robot_a));
        mRightData.add(new RightListBean(1,"XPOX ROBOT","(Android)",R.mipmap.img_car_b));
        if(mRightAdapter != null) {
            mRightAdapter.setData(mRightData);
        }
    }

    private void changeToothData(){
        if(mRightData == null){
            mRightData = new ArrayList<>();
        }
        mRightData.clear();
        mRightData.add(new RightListBean(0,"6WD Tooth","(Android)",R.mipmap.img_tooth_a));
        mRightData.add(new RightListBean(1,"XPOX Tooth","(Android)",R.mipmap.img_tooth_b));
        if(mRightAdapter != null) {
            mRightAdapter.setData(mRightData);
        }
    }

    private void changeCarData(){
        if(mRightData == null){
            mRightData = new ArrayList<>();
        }
        mRightData.clear();
        mRightData.add(new RightListBean(0,"6WD CAR","(Android)",R.mipmap.img_car_a));
        mRightData.add(new RightListBean(1,"XPOX CAR","(Android)",R.mipmap.img_car_b));
        mRightData.add(new RightListBean(1,"IPOX CAR","(Android)",R.mipmap.img_car_c));
        mRightData.add(new RightListBean(1,"IIPOX CAR","(Android)",R.mipmap.img_car_d));
        if(mRightAdapter != null) {
            mRightAdapter.setData(mRightData);
        }
    }


    @OnClick({R.id.iv_close})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.iv_close:
                onBackPressed();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        //获取第一次按键时间
        long mNowTime = System.currentTimeMillis();
        //比较两次按键时间差
        if ((mNowTime - mPressedTime) > 2000) {
            ArmsUtils.makeText(getApplicationContext(),
                    "再按一次退出" + ArmsUtils.getString(getApplicationContext(), R.string.public_app_name));
            mPressedTime = mNowTime;
        } else {
            super.onBackPressed();
        }
    }

}
